﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BareWeb.HtmlExtension;
using BareWeb.HtmlExtension.Extensions;
using Fiddler;
using System.Windows.Forms;

namespace BareWeb.HtmlResponseInspector
{
    public class HtmlResponseInspector : Inspector2, IResponseInspector2
    {
        HtmlInspectorControl _htmlInspector;
        private HtmlInfoHelper _validator;

        public override void AddToTab(TabPage o)
        {
            _htmlInspector = new HtmlInspectorControl(false);
            _validator = new HtmlInfoHelper();
            o.Text = "Html Inspector";
            o.Controls.Add(_htmlInspector);
            o.Controls[0].Dock = DockStyle.Fill;
        }

        public override int GetOrder()
        {
            return 0;
        }
        

        #region IBaseInspector2 Members

        public void Clear()
        {
            return;
        }

        public bool bDirty
        {
            get { return false; }
        }

        private bool m_ReadOnly;
        public bool bReadOnly
        {
            get
            {
                return m_ReadOnly;
            }
            set
            {
                m_ReadOnly = value;
            }
        }

        private byte[] m_body;
        public byte[] body
        {
            get
            {
                return m_body;
            }
            set
            {
                m_body = value;

                // get the details from the first selected session rather than "value" since that may still be encoded
                // plus the url is not available here
                var session = FiddlerApplication.UI.GetFirstSelectedSession();
                if (_validator.IsValid(session))
                {
                    _htmlInspector.DoInspect(session);
                }
                else
                {
                    _validator.DoLog("{0} did not have a content-type of HTML or had a zero size", session.url);
                }
            }
        }

        #endregion

        #region IResponseInspector2 Members

        public HTTPResponseHeaders headers
        {
            get
            {
                return null;
            }
            set
            {
                
            }
        }

        

        #endregion
    }
}